package cn.zhaoyuening.hnistalumnirecord.web.controller.admin;

import cn.zhaoyuening.hnistalumnirecord.utils.page.ResponceResult;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by 赵岳宁 on 2017/5/31.
 */
@RestController
@RequestMapping("admin/file/")
public class AdminFileUploadController {
    @RequestMapping(value = "fileUpload.do")
    public ResponceResult fileUpload(@RequestParam(value = "file") MultipartFile file, HttpSession session) throws IOException {
        if (file ==null||file.isEmpty()){
            return ResponceResult.failResult();
        }
        String rootPath = session.getServletContext().getRealPath("/");
        String filePath = "resource" + File.separatorChar + "uploadImg" + File.separatorChar+ UUID.randomUUID().toString();
        File savePath = new File(rootPath+filePath);
        FileUtils.copyInputStreamToFile(file.getInputStream(),savePath);
        String url = File.separatorChar+filePath;
        return ResponceResult.successResult(url);
    }
}
